//
//  XMLMapListModel.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/6/27.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XMLMapListModel.h"
#import "XXTool.h"

@implementation XMLMapListModel

+(void)checkMapWithMtype:(NSInteger)mtype
                complete:(void (^)(BOOL isNeedUpdate, NSMutableArray<XMLMapListModel *> *updateMapArray))block
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [DKHTTPRequest
     sendDataWithShortUrl:@"map_mtypelist"
              Param:[XXTool formatStringWithLanguage:[NSString stringWithFormat:@"userid=-1&passwd=&mtype=%ld",mtype]]
         ImageArray:nil
            TimeOut:10
         Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
             if (serverDic == nil)
             {
                block(NO,nil);
             }
             else if ([[serverDic objectForKey:@"code"] intValue]==200)
             {
                 NSArray *reList = [serverDic objectForKey:@"list"];
                 NSMutableArray <XMLMapListModel *>* list = [NSMutableArray array];
                 if ([reList isKindOfClass:[NSArray class]])
                 {
                     for (int i=0; i<reList.count; i++)
                     {
                         XMLMapListModel *temp = [XMLMapListModel modelWithDictionary:reList[i]];
                         temp.mtype = [NSString stringWithFormat:@"%ld",mtype];
                         if (([temp.mappath isEqualToString:@""] &&
                              [temp.mapphtml isEqualToString:@""]))
                         {
                             //该展馆地图被置为空，则说明不能跳转到该地图对应展馆，所以删除本地该展馆的地图
                             NSFileManager *manager = [NSFileManager defaultManager];
                             NSString *mapurl = [NSString stringWithFormat:@"%@/Documents/%ld%@.html",NSHomeDirectory(),mtype,temp.mapname];
                             if ([manager fileExistsAtPath:mapurl])
                             {
                                 [manager removeItemAtPath:mapurl error:nil];
                             }
                         }
                         else
                         {
                             NSInteger mapVision = [user integerForKey:[NSString stringWithFormat:@"%ld%@%@",mtype,mtypeMapVision,temp.mapname]];
                             //没有该地图或者本地地图的版本号比服务器低
                             if (mapVision == 0 || mapVision < [temp.mapversion integerValue])
                             {
                                 [list addObject:temp];
                             }
                         }
                     }
                 }
                 if (list.count>0) //有地图需要更新
                 {
                     block(YES,list);
                 }
                 else
                 {
                     block(NO,list);
                 }
             }
             return  YES;
         }
        IsBuffering:NO
    IsNeedReplaceWrap:NO
    IsUseUpLoadHead:NO];
}

+(void)requestMapListWithWithMtype:(NSInteger)mtype
                      completation:(void(^)(BOOL isSuccess,NSMutableArray<XMLMapListModel *>*list))block
{
    [DKHTTPRequest sendDataWithShortUrl:@"map_mtypelist"
                                  Param:[XXTool formatStringWithLanguage:[NSString stringWithFormat:@"userid=-1&passwd=&mtype=%ld",mtype]]
                             ImageArray:nil
                                TimeOut:10
                             Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                 if (serverDic==nil)
                                 {
                                     block(NO,nil);
                                     return NO;
                                 }
                                 else if ([[serverDic objectForKey:@"code"] intValue]==200)
                                 {
                                     NSArray *reList = [serverDic objectForKey:@"list"];
                                     NSMutableArray <XMLMapListModel *>* list = [NSMutableArray array];
                                     if ([reList isKindOfClass:[NSArray class]])
                                     {
                                         for (int i=0; i<reList.count; i++)
                                         {
                                             XMLMapListModel *temp = [XMLMapListModel modelWithDictionary:reList[i]];
                                             temp.mtype = [NSString stringWithFormat:@"%ld",mtype];
                                             if (([temp.mappath isEqualToString:@""]||[temp.mapphtml isEqualToString:@""]))
                                             {
                                                 
                                             }
                                             else
                                             {
                                                 [list addObject:temp];
                                             }
                                         }
                                     }
                                     block(YES,list);
                                     return  YES;
                                 }
                                 return NO;
                             }
                            IsBuffering:YES
                      IsNeedReplaceWrap:NO
                        IsUseUpLoadHead:NO];
}

@end
